﻿using System;
using System.Windows.Forms;


namespace Autorun
{
    public partial class PaneObjectExplorer : Autorun.PaneBase
    {
        private const string ROOT_NODE = "ROOT";
        private const string NODE_SERVER = "SERVER";
        private const string NODE_TASKS = "TASKS";
        private const string NODE_SCHEDULES = "SCHEDULES";
        private const string NODE_EVENTS = "EVENTS";
        private const string ICO_REFRESH = "icoRefresh";

        #region "Constructor and Form's Events Handlers"
        public PaneObjectExplorer()
        {
            InitializeComponent();
        }

        private void PaneObjectExplorer_Load(object sender, EventArgs e)
        {
            LoadIcons();
            mnuRefresh.Image = myImages.Images[ICO_REFRESH];
            myTree.ExpandAll();
        }
        #endregion

        #region "Properties"
        public ServerManagement Proxy
        {
            get { return _proxy; }
            set
            {
                _proxy = value;
                LoadObjectTree();
            }
        }
        #endregion

        #region "Methods"
        private void LoadIcons()
        {
            myImages.Images.Clear();
            myImages.Images.Add(ROOT_NODE, Properties.Resources.icoConnectedServer);
            myImages.Images.Add(ICO_REFRESH, Properties.Resources.icoRefresh);
            myImages.Images.Add(NODE_TASKS, Properties.Resources.icoTaskList);
            myImages.Images.Add(NODE_SCHEDULES, Properties.Resources.icoScheduleList);
            myImages.Images.Add(NODE_EVENTS, Properties.Resources.icoEvents);
            myImages.Images.Add(NODE_SERVER, Properties.Resources.icoRegisteredServers);
        }

        private void LoadObjectTree()
        {
            TreeNode root;
            TreeNode node;

            // === Refresh the navigation tree ===================================
            myTree.Nodes.Clear();
            if (_proxy == null)
            {
                myTree.Nodes.Add("(Not Connected)");
            }
            else
            {
                root = myTree.Nodes.Add(ROOT_NODE, _proxy.Information.MachineName, ROOT_NODE, ROOT_NODE);

                root.Nodes.Add(NODE_SERVER, "Configuration", NODE_SERVER, NODE_SERVER);

                // --- Retrieve the tasks ------------------------------------------
                root.Nodes.Add(NODE_TASKS, string.Format("Tasks ({0})", _proxy.Configuration.Tasks.Items.Count), NODE_TASKS, NODE_TASKS);

                // --- Retrieve the schedules --------------------------------------
                node = root.Nodes.Add(NODE_SCHEDULES, string.Format("Schedules ({0})", _proxy.Scheduler.Items.Count), NODE_SCHEDULES, NODE_SCHEDULES);

                /*
                query = "SELECT COUNT(*) FROM tblSchedule WHERE schType=1";
                count = (int)ExecuteScalar(query);
                node.Nodes.Add(NODE_SCHEDULE, string.Format("Startup ({0})", count), NODE_SCHEDULE, NODE_SCHEDULE);

                query = "SELECT COUNT(*) FROM tblSchedule WHERE schType=2";
                count = (int)ExecuteScalar(query);
                node.Nodes.Add(NODE_SCHEDULE, string.Format("Shutdown ({0})", count), NODE_SCHEDULE, NODE_SCHEDULE);

                query = "SELECT COUNT(*) FROM tblSchedule WHERE schType=4";
                count = (int)ExecuteScalar(query);
                node.Nodes.Add(NODE_SCHEDULE, string.Format("Daily ({0})", count), NODE_SCHEDULE, NODE_SCHEDULE);

                query = "SELECT COUNT(*) FROM tblSchedule WHERE schType=8";
                count = (int)ExecuteScalar(query);
                node.Nodes.Add(NODE_SCHEDULE, string.Format("Weekly ({0})", count), NODE_SCHEDULE, NODE_SCHEDULE);

                query = "SELECT COUNT(*) FROM tblSchedule WHERE schType=16";
                count = (int)ExecuteScalar(query);
                node.Nodes.Add(NODE_SCHEDULE, string.Format("Monthly ({0})", count), NODE_SCHEDULE, NODE_SCHEDULE);
                */

                // --- Retrieve the event log entries ------------------------------
                //query = "SELECT COUNT(*) FROM tblEvent";
                node = root.Nodes.Add(NODE_EVENTS, string.Format("Events ({0})", _proxy.Log.LogEntriesCount), NODE_EVENTS, NODE_EVENTS);

                myTree.ExpandAll();

                myTree.SelectedNode = root;
                myTree_Click(null, null);
            }
        }
        #endregion

        #region "Controls Events Handlers"
        private void myTree_MouseDown(object sender, MouseEventArgs e)
        {
            myTree.SelectedNode = myTree.HitTest(e.X, e.Y).Node;
        }

        private void myTree_Click(object sender, EventArgs e)
        {
            if (myTree.SelectedNode != null)
                switch (myTree.SelectedNode.Name)
                {
                    case ROOT_NODE:
                        _parent.ShowPane(typeof(ServerDashboard), new ShowPaneEventArgs(_proxy));
                        break;
                    case NODE_SERVER:
                        _parent.ShowPane(typeof(PaneServer), new ShowPaneEventArgs(_proxy));
                        break;
                    case NODE_TASKS:
                        _parent.ShowPane(typeof(PaneTasks), new ShowPaneEventArgs(_proxy));
                        break;
                    case NODE_SCHEDULES:
                        _parent.ShowPane(typeof(PaneSchedules), new ShowPaneEventArgs(_proxy));
                        break;
                    case NODE_EVENTS:
                        _parent.ShowPane(typeof(PaneLog), new ShowPaneEventArgs(_proxy));
                        break;
                    default:
                        break;
                }

        }

        private void mnuRefresh_Click(object sender, EventArgs e)
        {
            LoadObjectTree();
        }
        #endregion
    }
}
